﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Caching;
using TreaShares.Backoffice.DataLayer;

namespace TreaShares.Backoffice
{
    public partial class BackOffice : System.Web.UI.MasterPage
    {
        OrganisationManager.OrganisationBasicInfo orgBasicInfo;
        
        protected override void OnInit(EventArgs e)
        {
            string orgId = string.Empty;
            if (Request.Cookies["OrganisationId"] != null)
            {
                orgId = Server.HtmlEncode(Request.Cookies["OrganisationId"].Value);
                this.ClientChanged(orgId);
                this.OrganisationId = orgId;
                
            }
            else
            {
                this.lblOrganisationId.Text = "Problem to read cookie";
                this.lblOrganisationId.ForeColor = System.Drawing.Color.Red;
            }
            this.lblRole.Text = ((BasePage)this.Page).UserRole;
            base.OnInit(e);
            this.lblTitle.Text = this.Page.Title;
            this.Page.RegisterClientScriptBlock("userSettings",
                string.Format("<script> var userSettingDateFormat = '{0}';var defaultNewTransactionJS = {1}; var defaultReportingJS = {2}; var todayDate ='{3}' </script>",
                orgBasicInfo.UserSettings.DateFormatJS, orgBasicInfo.UserSettings.DefaultNewTransactionJS,
                orgBasicInfo.UserSettings.DefaultReportingJS,
                string.Format(orgBasicInfo.UserSettings.DateFormat, DateTime.Now)));
        }
        public void ClientChanged(string orgId)
        {
            if (HttpContext.Current.Cache[orgId] != null)
            {
                orgBasicInfo = HttpContext.Current.Cache[orgId] as OrganisationManager.OrganisationBasicInfo;
            }
            else
            {
                orgBasicInfo = OrganisationManager.GetOrganisationInfo(new Guid(orgId), Context.User.IsInRole("Administrator"));
                HttpContext.Current.Cache.Add(orgId, orgBasicInfo, null, DateTime.MaxValue, new TimeSpan(0, 35, 0), CacheItemPriority.Normal, null);
            }
            this.OrganisationId = orgId;

            if (string.IsNullOrEmpty(orgBasicInfo.Logo))
            {
                this.OrganisationName = orgBasicInfo.Name;
                this.imgClientLogo.Visible = false;
                this.lblOrganisationName.Visible = true;
            }
            else
            {
                this.OrganisationName = string.Empty;
                this.imgClientLogo.ImageUrl = "~/Images/Logos/" + orgBasicInfo.Logo;
                this.imgClientLogo.Visible = true;
            }
        }

        public OrganisationManager.OrganisationBasicInfo OrgBasicInfo
        {
            get { return orgBasicInfo; }
        }
        public string OrganisationName
        {
            set
            {
                this.lblOrganisationName.Text = value;
            }
        }

        public string OrganisationId
        {
            set
            {
                this.lblOrganisationId.Text = value;
                //this.hlOpenSite.NavigateUrl = string.Format("~/OrganisationSite/Tournaments.aspx?OrgId={0}", value);
            }
        }

        protected void Menu3_DataBound(object sender, EventArgs e)
        {
            Menu m = sender as Menu;
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                var clientReport = db.ClientReportsGet(this.OrgBasicInfo.Id);
                
                foreach (var item in clientReport)
                {
                    m.Items[4].ChildItems.Add(new MenuItem(item.DisplayName, string.Empty, string.Empty, "User/Reports.aspx?ReportId=" + item.Id.ToString()));
                }
                m.Items[4].NavigateUrl = string.Empty;
            }
        }


    }
}
